package cn.yunhe.controller;

import cn.yunhe.entity.Insurcate;
import cn.yunhe.service.InsurcateService;
import cn.yunhe.util.Result;
import cn.yunhe.util.UUIDGenerator;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 董稳钰
 * @date 2018/9/17 16:08
 */
@RestController
@RequestMapping("/insurcate")
public class InsurcateController {
    @Resource
    private InsurcateService insurcateService;

    /*===================================================================================*/
    @RequestMapping("/toInsurcateList")
    public ModelAndView toInsurList(){
        return new ModelAndView("insurcate_list");
    }
    @RequestMapping("/toAddInsurcate")
    public ModelAndView toAddInsurcate(){
        return new ModelAndView("insurcate_add");
    }
    @RequestMapping("/toupdateInsurcate")
    public ModelAndView toupdateInsurcate(Insurcate insurcate){
        Insurcate insurcate1=insurcateService.getInsurcateById(insurcate);
        ModelAndView mv=new ModelAndView("insurcate_update");
        mv.addObject("insurcate1",insurcate1);
        return mv;
    }

        //添加
    @RequestMapping("/toAddInsurcateAfter")
    public ModelAndView toAddInsurcateAfter(Insurcate insurcate) {
        /*获取UUId*/
        insurcate.setIuuid(UUIDGenerator.getUUID());
        insurcateService.addInsurcate(insurcate);
        return new ModelAndView("insurcate_list");
    }
    @RequestMapping("/toGetInsurcate")
    public Map<String,Object>  toGetInsurcate(int pageSize,int pageNum,String keyword ){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("keyword",keyword);
        Page page=insurcateService.findLikeInsurcates(map);
        map.put("page",page);
        map.put("totalPages",page.getPages());
        return map;

    }
    //删除保险
    @RequestMapping("/toDelInsurcate")
    public Result delInsurcate(Insurcate insurcate){
        insurcateService.delInsurcate(insurcate);
        return new Result();
    }
    //批量删除保险
    @RequestMapping("/delInsurcates")
    public ModelAndView delInsurcates(@RequestParam(value="iuuidList[]") String[]iuuidList){
        List<String>iuuids=Arrays.asList(iuuidList);
        insurcateService.delInsurcates(iuuids);
        return new ModelAndView("insurcate_list");
    }
   //获取所有上架的保险
    @RequestMapping("/getAllUseableInsurcate")
    public Map getAllUseableInsurcate(){
        Map<String,Object> map=new HashMap<String,Object>();
        List<Insurcate> allUseableInsurcate = insurcateService.getAllUseableInsurcate();
        map.put("allUseableInsurcate",allUseableInsurcate);
        return  map;
    }
    //修改保险
    @RequestMapping("/updateInsurcate")
   public Result updateInsurcate(Insurcate insurcate){
        System.out.println("**********************************************************88");
        System.out.println(insurcate.getComrate());
        insurcateService.updateInsurcate(insurcate);
        return new Result();
   }
    /*================================================================================*/
    public InsurcateService getInsurcateService() {
        return insurcateService;
    }

    public void setInsurcateService(InsurcateService insurcateService) {
        this.insurcateService = insurcateService;
    }
}
